import jpype
import time
from configs.config_manage import PROJECT_PATH


jvm_path = jpype.getDefaultJVMPath()  # jvm.dll路径
jar_dirs = PROJECT_PATH + r"\jar" + r"\myTest.jar"  # jar文件路径
jpype.startJVM(jvm_path, "-ea", "-Djava.class.path=%s" % jar_dirs, convertStrings=False)  # 开启Java虚拟机并读取jar包


def regist_sign(macid, ts, terminaltype=None, key=None, paydevicetype=None, shownum=None):
    SignUtil_SHA1_Class = jpype.JClass("com.ych.device.SignUtil_SHA1")  # 申请类

    # 参数
    MacID = macid  # 设备ID
    TerminalType = str(terminaltype)  # 设备类型

    if key is None:
        key = "C5D7C9FD354349B1A23D1DEE318D4E58"  # 程序定义的key
    else:
        key = key

    if paydevicetype is None:
        data = "macid=" + MacID + "&terminaltype=" + TerminalType + "&ts=" + str(ts)
    else:
        data = "macid=" + MacID + "&paydevicetype=" + str(paydevicetype) + "&shownum=" + str(shownum) + "&ts=" + str(ts)
    sign = str(SignUtil_SHA1_Class.genHMAC(data, key))  # 返回最终签名(格式为java的String，需要转为Python的str)
    # print(sign)
    return sign


def login_sign(terminaltype, macid, ts, appid, appkey, appver):
    SignUtil_SHA1_Class = jpype.JClass("com.ych.device.SignUtil_SHA1")  # 申请类

    # 参数
    MacID = macid  # 设备ID
    TerminalType = str(terminaltype)  # 设备类型
    AppID = appid
    key = appkey
    data = "appid="+AppID+"&appver="+str(appver)+"&macid="+MacID+"&terminaltype="+TerminalType+"&ts="+str(ts)

    sign = str(SignUtil_SHA1_Class.genHMAC(data, key))  # 返回最终签名(格式为java的String，需要转为Python的str)
    # print(sign)
    return sign

# jpype.shutdownJVM()

if __name__ == '__main__':
    regist_sign(terminaltype=3, macid="af2b2fc3c3e44154905740c7d4ab32a7821D", ts=int(time.time() * 1000))
    print(regist_sign(paydevicetype=2, macid="004CBE0E0016301C", ts=int(time.time() * 1000), shownum=164800))
